//
// ProcessManager.h
// Usage: Manage process in system, only manage, not care about parent-child 
//        relation ship
// CreateDate: 2009-04-16
// Author: MaJiuyue CaiRuicheng
//
#ifndef _PROCESSMANAGER_H_
#define _PROCESSMANAGER_H_

#include <type.h>
#include <const.h>
#include <Map.h>
#include <BitArray.h>
#include <SCI.h>
#include "Process.h"
#include "Thread.h"
#include "Context.h"

class CProcessManager : public Callable
{
private:
	/* Map of (pid, Process *) */
	Map<pid_t, Process *, 64> m_ProcessMap;

public:
	/* Init PM, and create process 0: init */
	CProcessManager();
	/* Temporialy do nothing */
	~CProcessManager();

public:
	/* get process descriptor by pid */
	Process * GetProcessByPid(pid_t pid);

	/*
	  Dumplicate parent process to create a new process.
	  User process should always calls this.
	*/
	Process * CreateProcess(pid_t parent);

	/*
	  Create a empty-none_parent process.
	  Only kernel calls this at boot time for module created.
	  User process should never touch this.

	  Default entry address is 0x100000, default eip is 0x80000000, kernel
	  should reset this by call Thread's member-function 
		SetRegisterValue(REGS_EIP, new_entry) 
		SetRegisterValue(REGS_EIP, stack_address)
	  before process can be schedule.

	  Parameters:
		priority	Process's priority
	*/
	Process * CreateProcessKNL(int priority);

	/* remove pid from ProcessTable, remove pid from Scheduler */
	void DestroyProcess(pid_t pid);

private:
	/* pid for next created process */
	pid_t m_NextPid;
	/* pid bitmap for pid allocation */
	BitArray m_PidBitmap;
	/* Get a pid */
	pid_t GetPid();
	/* Put a pid */
	void PutPid(pid_t pid);

	/* Add a created process to schedule list */
	void AddSchedule(Process * proc);
	/* Remove process from schedule list */
	void RemoveSchedule(Process * proc);
};

#endif

